import {
  queryPersonalPageDataReq,
  queryLeaguePageDataReq,
  queryClassPageDataReq,
  addItemReq,
  updateItemReq,
  deleteItemReq,
  toggleStateReq,
  queryItemInfoReq,
} from "./services";
import { namespace } from "./configFile";
import { message } from "antd";

export default {
  namespace,
  state: {
    init: false,
    data: {},
  },
  effects: {
    *pageInit({ params }, { call, put }) {
      // const baseData = yield call(queryLeaguePageDataReq, params)
      // yield put({
      //   type: 'save',
      //   payload: {
      //     ...baseData,
      //   }
      // })
    },
    *queryPageData({ params, callback }, { call, put }) {
      let res;
      if (params === "1") {
        res = yield call(queryPersonalPageDataReq, {});
      } else if (params === "2") {
        res = yield call(queryLeaguePageDataReq, {});
      } else {
        res = yield call(queryClassPageDataReq, {});
      }
      if (res) {
        callback && callback(res);
      }
    },

    *submitItem({ params, callback }, { call }) {
      console.log("params", params);
      if (!params.id || !params.courseType) {
        return;
      }
      let res = yield call(updateItemReq, params);
      console.log("res", res);

      if (res?.code === 200) {
        message.success(res.msg);
        callback && callback();
      } else {
        message.error(res.msg);
      }
    },

    *deleteItem({ params, callback }, { call }) {
      let res = yield call(deleteItemReq, params);
      if (res?.code === 200) {
        message.success(res.msg);
        callback && callback();
      } else {
        message.error(res.msg);
      }
    },

    *queryItemInfo({ params, callback }, { call }) {
      let res = yield call(queryItemInfoReq, params);
      if (res) {
        callback && callback(res);
      }
    },

    *toggleState({ params, callback }, { call }) {
      let res = yield call(toggleStateReq, params);
      if (res) {
        callback && callback();
      }
    },
  },
  reducers: {
    save(state: any, { payload }: any) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
